package com.forum.controller;

import com.forum.common.api.CommonResult;
import com.forum.service.LikeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "点赞接口")
@RestController
@RequiredArgsConstructor
public class LikeController {
    
    private final LikeService likeService;
    
    /**
     * 帖子类型
     */
    private static final int TYPE_POST = 1;
    
    /**
     * 评论类型
     */
    private static final int TYPE_COMMENT = 2;
    
    @Operation(summary = "点赞帖子")
    @PostMapping("/post/{id}/like")
    public CommonResult<Boolean> likePost(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        if (userId == null) {
            return CommonResult.unauthorized("请先登录");
        }
        boolean result = likeService.like(userId, id, TYPE_POST);
        return CommonResult.success(result);
    }
    
    @Operation(summary = "获取帖子点赞状态")
    @GetMapping("/post/{id}/like/status")
    public CommonResult<Boolean> getPostLikeStatus(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        if (userId == null) {
            return CommonResult.success(false);
        }
        boolean hasLiked = likeService.hasLiked(userId, id, TYPE_POST);
        return CommonResult.success(hasLiked);
    }
    
    @Operation(summary = "点赞评论")
    @PostMapping("/comment/{id}/like")
    public CommonResult<Boolean> likeComment(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        if (userId == null) {
            return CommonResult.unauthorized("请先登录");
        }
        boolean result = likeService.like(userId, id, TYPE_COMMENT);
        return CommonResult.success(result);
    }
    
    @Operation(summary = "获取评论点赞状态")
    @GetMapping("/comment/{id}/like/status")
    public CommonResult<Boolean> getCommentLikeStatus(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        if (userId == null) {
            return CommonResult.success(false);
        }
        boolean hasLiked = likeService.hasLiked(userId, id, TYPE_COMMENT);
        return CommonResult.success(hasLiked);
    }
} 